<?php

global $WSC;

require_once('tcms_vpt_page_wp.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/ci/ci_types/tcms_ci_article_dbc.class.php');


class TCMS_VPT_Article_Page_WP extends TCMS_VPT_Page_WP {
	function __construct(TCMS_VPage_DBC $vpage, TCMS_VPage_Template_DBC $vpt) {
		parent::__construct($vpage, $vpt, 'tcms_vpt_article_page');
	}


	protected function default_event() {
		$dbc = $this->get_dbc();

		$vpage_cis = TCMS_VPage_CI_DBC::load_cis_for_page($dbc, $this->get_vpage()->get_id());
		foreach($vpage_cis as $vpage_ci) {
			$this->prepare_ci_article($vpage_ci->get_ci_id());
			break;
		}

		return parent::default_event();
	}


	private function prepare_ci_article($ci_article_id) {
		$dbc = $this->get_dbc();

		$ci_article = new TCMS_CI_Article_DBC();
		if ($ci_article->load_by_id($dbc, $ci_article_id)) {
			$ci_article_prep = $this->prepare_article($ci_article);
			$this->export('ci_article', $ci_article_prep);

			if (trim($ci_article_prep['intro'])) {
				$this->export('meta_description', strip_tags($ci_article_prep['intro']));
			}

			$ci_id = $ci_article->get_ci()->get_id();
			$this->export('ci_id', $ci_article->get_ci()->get_id());

			$authors_attr_type = new TCMS_CI_Type_Attr_DBC();
			if (!$authors_attr_type->load_by_type_and_hid($dbc, $ci_article->get_ci()->get_type(), 'authors')) {
				throw new Tangra_Module_Exception('Cannot find "authors" CI_Type_Attr');
			}
			$this->export('authors_attr_type_id', $authors_attr_type->get_id());

			$authors_nomenclature_hid = $authors_attr_type->get_params();
			$authors_nomenclature = new TCMS_Nomenclature_DBC();
			if (!$authors_nomenclature->load_by_hid($dbc, $authors_nomenclature_hid)) {
				throw new Tangra_Module_Exception('Cannot find nomenclature with hid = '.$authors_nomenclature_hid);
			}


			$authors = TCMS_CIA_Nomenclature_DBC::select_all_by_ci_tid_nom($dbc, $ci_article->get_ci()->get_id(), $authors_attr_type->get_id(), $authors_nomenclature->get_id());
			$this->export('authors', $authors);

			$this->export_attachments($dbc, $ci_id);
			$this->export_images($dbc, $ci_id);
		}
	}


	private function prepare_article(TCMS_CI_Article_DBC $ci_article) {
		$ret['id'] = $ci_article->get_ci()->get_id();
		$ret['title_clean'] = strip_tags($ci_article->get_title()->get_value());
		$ret['title'] = $ci_article->get_title()->get_value();
		$ret['content'] = $ci_article->get_content()->get_value();
		$ret['intro'] = $ci_article->get_intro()->get_value();
		$ret['created'] = $ci_article->get_ci()->get_created();

		return $ret;
	}


	public static function get_short_text_description(DB_Connection $dbc, $vpage_id) {
		tangra_if_not_int_throw_e($vpage_id);

		$ret = '';

		$vpage_cis = TCMS_VPage_CI_DBC::load_cis_for_page($dbc, $vpage_id);
		if ($vpage_cis) {
			$ci_id = 0;
			foreach($vpage_cis as $vpage_ci) {
				$ci_id = $vpage_ci->get_ci_id();
				break;
			}
			$ci_article = new TCMS_CI_Article_DBC();
			if ($ci_article->load_by_id($dbc, $ci_id)) {
				$ret = strip_tags($ci_article->get_title()->get_value());
			}
		}

		return $ret;
	}


	public static function get_article_ci_for_page(DB_Connection $dbc, $vpage_id) {
		$ret = false;

		$vpage_cis = TCMS_VPage_CI_DBC::load_cis_for_page($dbc, $vpage_id);

		if ($vpage_cis) {
			$vpage_ci = current($vpage_cis);
			$ci_article = new TCMS_CI_Article_DBC();
			if ($ci_article->load_by_id($dbc, $vpage_ci->get_ci_id())) {
				$ret = $ci_article;
			}
		}

		return $ret;
	}
}